﻿using AnesSystem.Models;
using AnesSystem.Services;
using Common;
using MongoDB.Bson;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AnesSystem.ViewModels
{
    class ConfigDrugTypesCtrlViewModel : BaseViewModel<ConfigDrugTypesCtrlViewModel>
    {
        #region Variables
        private string _value = "";

        private ObservableCollection<ConfigDrugTypesModel> _displayConfigDrugTypesModels = new ObservableCollection<ConfigDrugTypesModel>();

        #endregion

        #region Constructor

        public ConfigDrugTypesCtrlViewModel()
        {}



        #endregion

        #region Properties

        public ObservableCollection<ConfigDrugTypesModel> DisplayConfigDrugTypesModels
        {
            get
            {
                _displayConfigDrugTypesModels.Clear();
                List<ConfigDrugTypesModel> lst = ConfigDrugTypesDataGridViewModel.Instance.ConfigDrugTypesModel.ToList().Where(O => O.Name.Contains(_value) || O.Code.Contains(_value)).ToList();
                foreach (var item in lst)
                {
                    item.PropertyChanged += Item_PropertyChanged;
                    _displayConfigDrugTypesModels.Add(item);
                }
                return _displayConfigDrugTypesModels;
         }
            set
            {
                ConfigDrugTypesDataGridViewModel.Instance.ConfigDrugTypesModel = value;
                RaisePropertyChanged("DisplayConfigDrugTypesModels");
            }
        }

        public string Value
        {
            get
            {
                return _value;
            }

            set
            {
                _value = value;
                RaisePropertyChanged("DisplayConfigDrugTypesModels");
            }
        }


        #endregion

        #region Event

        #endregion

        #region Private Methods
        private void Item_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            ConfigDrugTypesModelService.Instance.Update(sender as ConfigDrugTypesModel);
        }
        private void AddConfigDrugTypesModel(ConfigDrugTypesModel obj)
        {
            if (obj.Id != new ObjectId("000000000000000000000000"))
            {

                ConfigDrugTypesModelService.Instance.Update(obj);
                ConfigDrugTypesDataGridViewModel.Instance.ConfigDrugTypesModel.Add(obj);
            }
            else ConfigDrugTypesModelService.Instance.Insert(obj);


        }

        #endregion

        #region Public/Protected Methods
        public void DelConfigDrugTypesModel(ConfigDrugTypesModel configDrugTypesModel)
        {
            if (configDrugTypesModel != null)
            {
                ConfigDrugTypesDataGridViewModel.Instance.ConfigDrugTypesModel.Remove(configDrugTypesModel);
                RaisePropertyChanged("DisplayConfigDrugTypesModels");
                ConfigDrugTypesModelService.Instance.DeleteById(configDrugTypesModel.Id);
            }
        }

        public void AddRow()
        {
            ConfigDrugTypesModel model = new ConfigDrugTypesModel();
            DisplayConfigDrugTypesModels.Insert(0, model);
            model.PropertyChanged += Model_PropertyChanged;
        }
        #endregion

        #region EventHandlers
        private void Model_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            AddConfigDrugTypesModel(sender as ConfigDrugTypesModel);
        }

        #endregion

        #region Command

        #region ChangeDoctorCommand

        #endregion

        #region OpenPopupCommand

        #endregion
        #endregion

    }
}
